<?php
$loader->requireOnce('includes/EwhValuatePrice/IValuatePriceMethod.class.php');
$loader->requireOnce('includes/EwhValuatePrice/ValuatedPricesResult.class.php');
/**
 * pro for valuated engine. This class process valuating prices by latestPrice
 * @package modules.dept_pharmacy.includes.EwhValuatePrice
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class ValuatePriceByLatestPrice implements IValuatePriceMethod {

    private $logger ;

    function __construct() {

        $this->logger = LoggerManager::getLogger('modules.dept_pharmacy.includes.EwhValuatePrice.ValuatePriceByLatestPrice');

    }

    public function valuatePrices($metaData=array(), $valuatingIdList=array()) {
        $this->logger->info('start valuatePrices ');
        $resultArr = array();

        foreach ($valuatingIdList as $item) {
            //get imported price
            $phaEwhExticketDetailObj = Celini::newOrdo('PhaEwhImticketDetail');
            $phaEwhExticketDetailObj->setup($item);
           // if ($phaEwhExticketDetailObj->get('valuated') == 0) {
                $lastedItem = $phaEwhExticketDetailObj->getLastedImportedItem();
                if ($lastedItem != null) {
                    $result = new ValuatedPricesResult();
                    $result->setImticketDetailId($item);
                    $result->setRefImticketDetailId($lastedItem['pha_ewh_imticket_detail_id']);

                    $servicePrice = $lastedItem['im_unit_price'];

                    if ($metaData['enum_rate_type'] == EnumerationConstant::PHA_VALUATE_RATE_TYPE_PERCENT_KEY) {
                        $servicePrice = $lastedItem['im_unit_price']
                        + (($metaData['service_rate'] * $lastedItem['im_unit_price'])/100);
                    } else if ($metaData['enum_rate_type'] == EnumerationConstant::PHA_VALUATE_RATE_TYPE_VND_KEY) {
                        $servicePrice = $lastedItem['im_unit_price'] + $metaData['service_rate'] ;
                    }

                    $insurrancePrice = $lastedItem['im_unit_price'];
                    if ($metaData['enum_rate_type'] == EnumerationConstant::PHA_VALUATE_RATE_TYPE_PERCENT_KEY) {
                        $insurrancePrice = $lastedItem['im_unit_price']
                        + (($metaData['insurance_rate'] * $lastedItem['im_unit_price'])/100);
                    } else if ($metaData['enum_rate_type'] == EnumerationConstant::PHA_VALUATE_RATE_TYPE_VND_KEY) {
                        $insurrancePrice = $lastedItem['im_unit_price'] + $metaData['insurance_rate'] ;
                    }

                    $result->setServicePrice($servicePrice);
                    $result->setInsurancePrice($insurrancePrice);
                    $result->setRefImticketDetailId($item);
                    $resultArr[] = $result;
                    /*
                }  else {
                    $this->logger->info('could not valuate for this item [' . $item . ']');
                }*/
                //calculate
                //add result into result array
            }
        }
         


        $this->logger->info('finish valuatePrices ');
        return $resultArr;
    }

}
?>